package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.domain.DailyTrainExample;
import com.myc.train.business.domain.TrainStation;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.DailyTrainStation;
import com.myc.train.business.domain.DailyTrainStationExample;
import com.myc.train.business.mapper.DailyTrainStationMapper;
import com.myc.train.business.req.DailyTrainStationQueryReq;
import com.myc.train.business.req.DailyTrainStationSaveReq;
import com.myc.train.business.resp.DailyTrainStationQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainStationService {

    @Resource
    private DailyTrainStationMapper dailyTrainStationMapper;

    @Resource
    private TrainStationService trainStationService;

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainStationService.class);
    public void save(DailyTrainStationSaveReq req){
        DateTime now = DateTime.now();
        DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(req, DailyTrainStation.class);

        if(ObjectUtil.isNull(dailyTrainStation.getId())){
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.insert(dailyTrainStation);
        }else{
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.updateByPrimaryKey(dailyTrainStation);
        }
    }

    public PageResp<DailyTrainStationQueryResp> queryList(DailyTrainStationQueryReq req){
       //定义查询条件
        DailyTrainStationExample dailyTrainStationExample = new DailyTrainStationExample();
        //根据id倒序排序
        dailyTrainStationExample.setOrderByClause("date desc , train_code asc , `index` asc");
        //创建查询对象
        DailyTrainStationExample.Criteria criteria = dailyTrainStationExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<DailyTrainStation> dailyTrainStations = dailyTrainStationMapper.selectByExample(dailyTrainStationExample);
        //new一个PageInfo并将结果放进来
        PageInfo<DailyTrainStation> dailyTrainStationPageInfo = new PageInfo<>(dailyTrainStations);
        //类拷贝
        List<DailyTrainStationQueryResp> queryResps = BeanUtil.copyToList(dailyTrainStations, DailyTrainStationQueryResp.class);
        //new一个PageResp
        PageResp<DailyTrainStationQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(dailyTrainStationPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        dailyTrainStationMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public void genDailyTrain(Date date,String trainCode){
        DateTime now = DateTime.now();
        //删除当日该车次的信息
        DailyTrainStationExample dailyTrainStationExample = new DailyTrainStationExample();
        dailyTrainStationExample.createCriteria().andDateEqualTo(date).andTrainCodeEqualTo(trainCode);
        dailyTrainStationMapper.deleteByExample(dailyTrainStationExample);
        //获取该车次所有车站信息
        List<TrainStation> trainStations = trainStationService.selectByCode(trainCode);
        //非空校验
        if(CollUtil.isEmpty(trainStations)){
            LOG.info("当日改车次站点为空");
        }
        for(TrainStation trainStation : trainStations){
            //新增当日车次
            DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(trainStation, DailyTrainStation.class);
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStation.setDate(date);
            dailyTrainStationMapper.insert(dailyTrainStation);
        }
    }

}
